﻿using DreamCMS.BLL.Factory.Interface;
using DreamCMS.Entity;
using DreamCMS.Entity.Common;
using DreamCMS.WebCore.Common;
using DreamCMS.WebCore.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.DAL.DataAccessObject
{
    public class UserDAO:IUserDAO
    {
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user">用户信息实体</param>
        /// <returns>返回刚刚添加的用户ID</returns>
        public long AddUser(UserInfo user)
        {
            string sql = @"insert into [dm_Users]([type],[username],[name],[password],[email],[siteurl],[avatarurl],[description],[displayorder],[status],[postcount],[commentcount],[createdate]) values(@Type,@UserName,@Name,@Password,@Email,@SiteUrl,@AvatarUrl,@Description,@DisplayOrder,@Status,@PostCount,@CommentCount,@CreateDate)";
            OleDbParameter[] paras = { 
                                     AccessHelper.MakeParam("@Type",OleDbType.BigInt,8,ParameterDirection.Input,user.Type),
                                     AccessHelper.MakeParam("@UserName",OleDbType.VarWChar,50,ParameterDirection.Input,user.UserName),
                                     AccessHelper.MakeParam("@Name",OleDbType.VarWChar,50,ParameterDirection.Input,user.Name),
                                     AccessHelper.MakeParam("@Password",OleDbType.VarWChar,50,ParameterDirection.Input,user.Password),
                                     AccessHelper.MakeParam("@Email",OleDbType.VarWChar,50,ParameterDirection.Input,user.Email),
                                     AccessHelper.MakeParam("@SiteUrl",OleDbType.VarWChar,255,ParameterDirection.Input,user.Siteurl),
                                     AccessHelper.MakeParam("@AvatarUrl",OleDbType.VarWChar,255,ParameterDirection.Input,user.Avatarurl),
                                     AccessHelper.MakeParam("@Description",OleDbType.VarWChar,255,ParameterDirection.Input,user.Description),
                                     AccessHelper.MakeParam("@DisplayOrder",OleDbType.BigInt,8,ParameterDirection.Input,user.Displayorder),
                                     AccessHelper.MakeParam("@Status",OleDbType.BigInt,8,ParameterDirection.Input,user.Status),
                                     AccessHelper.MakeParam("@PostCount",OleDbType.BigInt,8,ParameterDirection.Input,user.Postcount),
                                     AccessHelper.MakeParam("@CommentCount",OleDbType.BigInt,8,ParameterDirection.Input,user.Commentcount),
                                     AccessHelper.MakeParam("@CreateDate",OleDbType.Date,8,ParameterDirection.Input,user.Createdate)
                                     };
            //TODO:上面的日期size是8吗
            long rel= AccessHelper.ExecuteNonQuery(CommandType.Text, sql, paras);
            if(rel>0)
            {
                object lastId=AccessHelper.ExecuteScalar(CommandType.Text, "select top 1 [userid] from [dm_Users] order by userid desc");
                return StringHelper.ObjectToInt64(lastId, 0);
            }
            return 0;
        }


        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user">用户信息实体</param>
        /// <returns>返回更新影响行数</returns>
        public long UpdateUser(UserInfo user)
        {
             string sql = @"update [dm_Users] set [usertype]=@Type,[username]=@UserName,[name]=@Name,[password]=@Password,[email]=@Email,[siteurl]=@SiteUrl,[avatarurl]=@AvatarUrl,[description]=@Desciption,[displayorder]=@DisplayOrder,[status]=@Status,[postcount]=@PostCount,[commentcount]=@CommentCount,[createdate]=@CreateDate";
             OleDbParameter[] paras = { 
                                     AccessHelper.MakeParam("@Type",OleDbType.BigInt,8,ParameterDirection.Input,user.Type),
                                     AccessHelper.MakeParam("@UserName",OleDbType.VarWChar,50,ParameterDirection.Input,user.UserName),
                                     AccessHelper.MakeParam("@Name",OleDbType.VarWChar,50,ParameterDirection.Input,user.Name),
                                     AccessHelper.MakeParam("@Password",OleDbType.VarWChar,50,ParameterDirection.Input,user.Password),
                                     AccessHelper.MakeParam("@Email",OleDbType.VarWChar,50,ParameterDirection.Input,user.Email),
                                     AccessHelper.MakeParam("@SiteUrl",OleDbType.VarWChar,255,ParameterDirection.Input,user.Siteurl),
                                     AccessHelper.MakeParam("@AvatarUrl",OleDbType.VarWChar,255,ParameterDirection.Input,user.Avatarurl),
                                     AccessHelper.MakeParam("@Description",OleDbType.VarWChar,255,ParameterDirection.Input,user.Description),
                                     AccessHelper.MakeParam("@DisplayOrder",OleDbType.BigInt,8,ParameterDirection.Input,user.Displayorder),
                                     AccessHelper.MakeParam("@Status",OleDbType.BigInt,8,ParameterDirection.Input,user.Status),
                                     AccessHelper.MakeParam("@PostCount",OleDbType.BigInt,8,ParameterDirection.Input,user.Postcount),
                                     AccessHelper.MakeParam("@CommentCount",OleDbType.BigInt,8,ParameterDirection.Input,user.Commentcount),
                                     AccessHelper.MakeParam("@CreateDate",OleDbType.Date,8,ParameterDirection.Input,user.Createdate)
                                     };
             return AccessHelper.ExecuteNonQuery(CommandType.Text, sql, paras);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回影响行数</returns>
        public long DeleteUser(long userId)
        {
            string sql = "delete from [dm_Users] where [userid]=@UserId";
            OleDbParameter[] paras=new OleDbParameter[]{AccessHelper.MakeParam("@UserId",OleDbType.BigInt,8,ParameterDirection.Input,userId)};
            return AccessHelper.ExecuteNonQuery(CommandType.Text, sql, paras);
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns>用户实体的集合</returns>
        public List<UserInfo> GetAllUsers()
        {
            string sql = "select * from [dm_Users] order by userid desc";
            DataSet ds=AccessHelper.ExecuteDataSet(CommandType.Text, sql);
            return DataSetToUserInfo(ds);

        }
        /// <summary>
        /// 判断用户名是否存在
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>是否存在</returns>
        public bool IsUserExist(string userName)
        {
            string sql = "select count(*) from [dm_Users] where [userid]=@userid";
            OleDbParameter[] paras = { AccessHelper.MakeParam("@userid",OleDbType.VarWChar,50,ParameterDirection.Input,userName)};
            long rel = Convert.ToInt64(AccessHelper.ExecuteScalar(CommandType.Text, sql));
            if(rel>0)
            {
                return true;
            }
            return false;

        }


        private List<UserInfo> DataSetToUserInfo(DataSet ds)
        {
            List<UserInfo> ls = new List<UserInfo>();
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                UserInfo user = new UserInfo();
                user.Userid = Convert.ToInt64(dr["userid"]);
                user.Type = Convert.ToInt64(dr["usertype"]);
                user.UserName = Convert.ToString(dr["username"]);
                user.Name = Convert.ToString(dr["name"]);
                user.Password = Convert.ToString(dr["password"]);
                user.Email = Convert.ToString(dr["email"]);
                user.Siteurl = Convert.ToString(dr["siteurl"]);
                user.Avatarurl = Convert.ToString(dr["avatarurl"]);
                user.Description = Convert.ToString(dr["description"]);
                user.Displayorder = Convert.ToInt64(dr["displayorder"]);
                user.Status = (UserStatus)Convert.ToInt64(dr["status"]);
                user.Postcount = Convert.ToInt64(dr["postcount"]);
                user.Commentcount = Convert.ToInt64(dr["commentcount"]);
                user.Createdate = Convert.ToDateTime(dr["createdate"]);
                ls.Add(user);
            }
            return ls;
        }
    }
}
